/**
 * === Javascript 模块 ======================
 * Copyright (c) 2016 BrandWake, All rights reserved.
 *
 * @version 1.0
 * @author BrandWake
 * @description 工具类
 * ---2016/10/12.----------------------------------------
 */

var ShopUtil = (function (u) {

    u.initDateFormat = function () {
        Date.prototype.Format = function (fmt) {
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "h+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }
    };


    u.generateTable = function (args) {
        this.initDateFormat();
        var dd = {
            method: 'GET',//默认使用GET方法
            className: 'text-c',
            data:'',
            columns: [],
            render: [],
            table: $('.table-sort'),
            after: function () {

            }
        };
        var options = $.extend(true, {}, dd, args);
        var COLUMNS = [];
        for (var index in options.columns) {
            var item = {};
            item.data = options.columns[index];
            item.className = options.className;
            COLUMNS.push(item);
        }

        for (var i in options.render) {
            var renderItem = options.render[i];
            COLUMNS[renderItem.index].render = renderItem.render;
        }
        console.log(COLUMNS);
        if (options.table.length == 0) {
            return;
        }
        var table = options.table.dataTable({
            "bStateSave": true,//状态保存
            "ordering": false,
            "searching": false,
            ajax: {
                url: options.url,
                type: options.method,
                data:options.data
            },
            columns: COLUMNS,
            initComplete: function () {
            	var res = arguments[arguments.length -1];
                var table = $('.table-sort').DataTable();
                options.after(table, res);
            }
        });
    };

    u.SERVER = function (url, method, param, callback) {
        $.ajax({
            url: url,
            type: method,
            data: param,
            success: function (data) {
                callback(data);
            }
        })
    };


    u.formToJSON = function (args) {
        var dd = {
            form: $('form')
        };
        var options = $.extend(true, {}, dd, args);
        if (options.form == null || options.form.length == 0) {
            return;
        }

        var result = {};
        options.form.find('input,textarea,select').each(function () {
            var name = $(this).attr('name');
            if ("" == name || undefined == name) return;
            if(this.type == 'radio' && !this.checked)return;
            result[name] = $(this).val();
        });
        return result;
    };
    
    u.tableQuery=function(oTable,param, successCallback){
		 oTable.settings()[0].ajax.data = param;
		 oTable.ajax.reload(function(res){
			 $.isFunction(successCallback) && successCallback(res);
		 });
    }
    
    return u;
})(window.ShopUtil || {});